<?php
class CityDetailsController extends AppController {

	var $name = 'CityDetails';

	function admin_index() {
		$this->CityDetail->recursive = 0;
		$this->set('cityDetails', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid city detail', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('cityDetail', $this->CityDetail->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->CityDetail->create();
			if ($this->CityDetail->save($this->data)) {
				$this->Session->setFlash(__('The city detail has been saved', true));
				$this->redirect(array('controller'=>'cities', 'action' => 'view', $this->data['CityDetail']['city_id']));
			} else {
				$this->Session->setFlash(__('The city detail could not be saved. Please, try again.', true));
			}
		}
		$cities = $this->CityDetail->City->find('list');
		$this->set(compact('cities'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid city detail', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->CityDetail->save($this->data)) {
				$this->Session->setFlash(__('The city detail has been saved', true));
				$this->redirect(array('controller'=>'cities', 'action' => 'view', $this->data['CityDetail']['city_id']));
			} else {
				$this->Session->setFlash(__('The city detail could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->CityDetail->read(null, $id);
		}
		$cities = $this->CityDetail->City->find('list');
		$this->set(compact('cities'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for city detail', true));
			$this->redirect(array('action'=>'index'));
		}
		$dt = $this->CityDetail->read(null, $id);
		$cid = $dt['CityDetail']['city_id'];
		if ($this->CityDetail->delete($id)) {
			$this->Session->setFlash(__('City detail deleted', true));
			$this->redirect(array('controller'=>'cities', 'action' => 'view', $cid));
		}
		$this->Session->setFlash(__('City detail was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function manager_index() {
		$this->CityDetail->recursive = 0;
		
		$user = $this->Auth->user();
		
		$cities = $this->CityDetail->City->citiesByUser($user["User"]["id"]);
		//var_dump($cities);
		
		//get cities string
		foreach($cities as $city)
		{
			$arr_city_id[] = $city["City"]["id"];
		}
		//var_dump($arr_city_id);
		$str_cities = implode(",", $arr_city_id);
		//echo $str_cities;
		
		$this->paginate = array(
				'conditions' => array('CityDetail.city_id' => $arr_city_id)				
			);
		
		$city_id = !empty($this->params['form']['filter_city']) && $this->params['form']['filter_city'] != "" ? $this->params['form']['filter_city'] : 0;
		if($city_id)
		{
			$this->paginate = array(
				'conditions' => array('CityDetail.city_id' => $city_id)				
			);
		}


		$this->set('cities', $cities);
		$this->set('city_id', $city_id);
		
		$this->set('cityDetails', $this->paginate());
	}

	function manager_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid city detail', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('cityDetail', $this->CityDetail->read(null, $id));
	}

	function manager_add() {
		$user = $this->Auth->user();
		Controller::loadModel('User');
		if (!empty($this->data)) {
			
			
			//check if the user has accessibility to city
			if(!$this->User->isUserCity($user["User"]["id"], $this->data["CityDetail"]["city_id"]))
			{
				$this->Session->setFlash(__('Manager doesn\'t has role on this city', true));
				$this->redirect(array('action' => 'index'));
			}
			
			$this->CityDetail->create();
			if ($this->CityDetail->save($this->data)) {
				$this->Session->setFlash(__('The city detail has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The city detail could not be saved. Please, try again.', true));
			}
		}
		//get cities		
		$cs = $this->CityDetail->City->citiesByUser($user["User"]["id"]);
		foreach($cs as $city)
		{
			$cities[$city["City"]["id"]] = $city["City"]["name"];
		}
		
		$this->set(compact('cities'));
	}

	function manager_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid city detail', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			
			//check if the user has accessibility to city
			$user = $this->Auth->user();
			Controller::loadModel('User');
			if(!$this->User->isUserCity($user["User"]["id"], $this->data["CityDetail"]["city_id"]))
			{
				$this->Session->setFlash(__('Manager doesn\'t has role on this city', true));
				$this->redirect(array('action' => 'index'));
			}
			
			if ($this->CityDetail->save($this->data)) {
				$this->Session->setFlash(__('The city detail has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The city detail could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			//check if the user has accessibility to city
			$user = $this->Auth->user();
			$this->data = $this->CityDetail->read(null, $id);
			Controller::loadModel('User');
			if(!$this->User->isUserCity($user["User"]["id"], $this->data["CityDetail"]["city_id"]))
			{
				$this->Session->setFlash(__('Manager doesn\'t has role on this city', true));
				$this->redirect(array('action' => 'index'));
			}	
		}
		//get cities
		$user = $this->Auth->user();
		$cs = $this->CityDetail->City->citiesByUser($user["User"]["id"]);
		foreach($cs as $city)
		{
			$cities[$city["City"]["id"]] = $city["City"]["name"];
		}
		
		//var_dump($cities);
		$this->set(compact('cities'));
	}

	function manager_delete($id = null) {
		//check if the user has accessibility to city
		$user = $this->Auth->user();
		$branch = $this->CityDetail->read(null, $id);
		Controller::loadModel('User');
			if(!$this->User->isUserCity($user["User"]["id"], $branch["CityDetail"]["city_id"]))
			{
				$this->Session->setFlash(__('Manager doesn\'t has role on this city', true));
				$this->redirect(array('action' => 'index'));
			}
		
		
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for city detail', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->CityDetail->delete($id)) {
			$this->Session->setFlash(__('City detail deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('City detail was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
